package com.guodu.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.guodu.dao.MaterialKcjgDao;
import com.guodu.dao.UnitsDao;
import com.guodu.dao.UserInfoDao;
import com.guodu.entity.MaterialKcjg;
import com.guodu.entity.MaterialkcjgStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.service.MaterialKcjgService;
import com.guodu.vo.MaterialKcjgQueryVo;
import com.guodu.vo.MaterialKcjgVo;




@Service("materialKcjgService")
public class MaterialKcjgServiceImpl implements MaterialKcjgService {

	
	@Resource
	private MaterialKcjgDao materialKcjgDao;
	
	@Resource
	private UnitsDao unitsDao;
	
	@Resource
	private UserInfoDao userInfoDao;
	
	
	@Override
	public Map<String, MaterialKcjgVo> findByVo(MaterialKcjgQueryVo mkqv, Units units) {
		// TODO Auto-generated method stub
		List<MaterialKcjg> list = materialKcjgDao.findByVo(mkqv,units);
		List<MaterialKcjgVo> materialKcjgVos = new ArrayList<MaterialKcjgVo>();
		for(MaterialKcjg m :list){
			MaterialKcjgVo materialKcjgVo = new MaterialKcjgVo(m);
			UserInfo userInfo  = userInfoDao.findById(m.getCreatePeople());
			materialKcjgVo.setUserName(userInfo.getUserName());
			if(userInfo.getUnitsId()!=null){
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			if(unitsInfo!=null){
				materialKcjgVo.setUnitsLevel(unitsInfo.getUnitsLevel());
				materialKcjgVo.setUnitsName(unitsInfo.getUnitsName());
				materialKcjgVo.setIndustry(unitsInfo.getIndustry());
				materialKcjgVo.setUnitsId(unitsInfo.getUnitsId());
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					materialKcjgVo.setFunitsName(funitsInfo.getUnitsName());
					materialKcjgVo.setFunitsId(funitsInfo.getUnitsId());
				}
			}
			}
			materialKcjgVos.add(materialKcjgVo);
		}
		Map<String, MaterialKcjgVo> map = new HashMap<String, MaterialKcjgVo>();
		map.put("all", new MaterialKcjgVo());
		List<Units> funitses = unitsDao.findAllFunits();
		for(Units units1 : funitses){
			map.put(units1.getUnitsId().toString()+"all", new MaterialKcjgVo());
		}
		for(MaterialKcjgVo view:materialKcjgVos){
			this.sum(map.get("all"), view);
			this.sum(map.get(view.getUnitsId()==null?"group":view.getFunitsId().toString()+"all"), view);
   			if(view.getFunitsId()!=null){
				if(view.getFunitsId().intValue()==view.getUnitsId().intValue()){
					this.sum(map.get(view.getFunitsId()+"xiaoji"),view);
				}else{
					MaterialKcjgVo viewVo1 = new MaterialKcjgVo(view);
 					viewVo1.setFunitsName(unitsDao.findById(view.getFunitsId()).getUnitsName());
					viewVo1.setUnitsName(unitsDao.findById(view.getUnitsId()).getUnitsName());
					map.put(view.getUnitsId().toString(), new MaterialKcjgVo(view));
				}
			}
		}
		return map;
	}


	private MaterialKcjgVo sum(MaterialKcjgVo view1, MaterialKcjgVo view2) {
		// TODO Auto-generated method stub
		if(view1.getAllMoneycl()!=null){
			view1.setAllMoneycl(view1.getAllMoneycl().add(view2.getAllMoneycl()!=null?view2.getAllMoneycl():new BigDecimal(0)));
			}else{
				view1.setAllMoneycl(view2.getAllMoneycl()!=null?view2.getAllMoneycl():new BigDecimal(0));
			}
		if(view1.getAllMoneysb()!=null){
			view1.setAllMoneysb(view1.getAllMoneysb().add(view2.getAllMoneysb()!=null?view2.getAllMoneysb():new BigDecimal(0)));
			}else{
				view1.setAllMoneysb(view2.getAllMoneysb()!=null?view2.getAllMoneysb():new BigDecimal(0));
			}
		if(view1.getAllMoneybj()!=null){
			view1.setAllMoneybj(view1.getAllMoneybj().add(view2.getAllMoneybj()!=null?view2.getAllMoneybj():new BigDecimal(0)));
			}else{
				view1.setAllMoneybj(view2.getAllMoneybj()!=null?view2.getAllMoneybj():new BigDecimal(0));
			}
		if(view1.getAllMoneykc()!=null){
			view1.setAllMoneykc(view1.getAllMoneykc().add(view2.getAllMoneykc()!=null?view2.getAllMoneykc():new BigDecimal(0)));
			}else{
				view1.setAllMoneykc(view2.getAllMoneykc()!=null?view2.getAllMoneykc():new BigDecimal(0));
			}
		if(view1.getOneYearbj()!=null){
			view1.setOneYearbj(view1.getOneYearbj().add(view2.getOneYearbj()!=null?view2.getOneYearbj():new BigDecimal(0)));
			}else{
				view1.setOneYearbj(view2.getOneYearbj()!=null?view2.getOneYearbj():new BigDecimal(0));
			}
		if(view1.getOneYearcl()!=null){
			view1.setOneYearcl(view1.getOneYearcl().add(view2.getOneYearcl()!=null?view2.getOneYearcl():new BigDecimal(0)));
			}else{
				view1.setOneYearcl(view2.getOneYearcl()!=null?view2.getOneYearcl():new BigDecimal(0));
			}
		if(view1.getOneYearsb()!=null){
			view1.setOneYearsb(view1.getOneYearsb().add(view2.getOneYearsb()!=null?view2.getOneYearsb():new BigDecimal(0)));
			}else{
				view1.setOneYearsb(view2.getOneYearsb()!=null?view2.getOneYearsb():new BigDecimal(0));
			}
		if(view1.getOneYearkc()!=null){
			view1.setOneYearkc(view1.getOneYearkc().add(view2.getOneYearkc()!=null?view2.getOneYearkc():new BigDecimal(0)));
			}else{
				view1.setOneYearkc(view2.getOneYearkc()!=null?view2.getOneYearkc():new BigDecimal(0));
			}
		if(view1.getTwoYearbj()!=null){
			view1.setTwoYearbj(view1.getTwoYearbj().add(view2.getTwoYearbj()!=null?view2.getTwoYearbj():new BigDecimal(0)));
			}else{
				view1.setTwoYearbj(view2.getTwoYearbj()!=null?view2.getTwoYearbj():new BigDecimal(0));
			}
		if(view1.getTwoYearcl()!=null){
			view1.setTwoYearcl(view1.getTwoYearcl().add(view2.getTwoYearcl()!=null?view2.getTwoYearcl():new BigDecimal(0)));
			}else{
				view1.setTwoYearcl(view2.getTwoYearcl()!=null?view2.getTwoYearcl():new BigDecimal(0));
			}
		if(view1.getTwoYearsb()!=null){
			view1.setTwoYearsb(view1.getTwoYearsb().add(view2.getTwoYearsb()!=null?view2.getTwoYearsb():new BigDecimal(0)));
			}else{
				view1.setTwoYearsb(view2.getTwoYearsb()!=null?view2.getTwoYearsb():new BigDecimal(0));
			}
		if(view1.getTwoYearkc()!=null){
			view1.setTwoYearkc(view1.getTwoYearkc().add(view2.getTwoYearkc()!=null?view2.getTwoYearkc():new BigDecimal(0)));
			}else{
				view1.setTwoYearkc(view2.getTwoYearkc()!=null?view2.getTwoYearkc():new BigDecimal(0));
			}
		if(view1.getThreeYearbj()!=null){
			view1.setThreeYearbj(view1.getThreeYearbj().add(view2.getThreeYearbj()!=null?view2.getThreeYearbj():new BigDecimal(0)));
			}else{
				view1.setThreeYearbj(view2.getThreeYearbj()!=null?view2.getThreeYearbj():new BigDecimal(0));
			}
		if(view1.getThreeYearsb()!=null){
			view1.setThreeYearsb(view1.getThreeYearsb().add(view2.getThreeYearsb()!=null?view2.getThreeYearsb():new BigDecimal(0)));
			}else{
				view1.setThreeYearsb(view2.getThreeYearsb()!=null?view2.getThreeYearsb():new BigDecimal(0));
			}
		if(view1.getThreeYearcl()!=null){
			view1.setThreeYearcl(view1.getThreeYearcl().add(view2.getThreeYearcl()!=null?view2.getThreeYearcl():new BigDecimal(0)));
			}else{
				view1.setThreeYearcl(view2.getThreeYearcl()!=null?view2.getThreeYearcl():new BigDecimal(0));
			}
		if(view1.getThreeYearkc()!=null){
			view1.setThreeYearkc(view1.getThreeYearkc().add(view2.getThreeYearkc()!=null?view2.getThreeYearkc():new BigDecimal(0)));
			}else{
				view1.setThreeYearkc(view2.getThreeYearkc()!=null?view2.getThreeYearkc():new BigDecimal(0));
			}
		return view1;
	}


	@Override
	public void examine(String ids, String rs, String beforeStatus,
			Long status, UserInfo user) {
		// TODO Auto-generated method stub
		String[] idses = ids.split(",");
		String[] beforeStatuses = beforeStatus.split(",");
		List<MaterialkcjgStatus> entities = new ArrayList<MaterialkcjgStatus>();
		for(int i=0;i<idses.length;i++){
			MaterialkcjgStatus entity = new MaterialkcjgStatus();
			entity.setAfterStatus(status);
			entity.setBeforeStatus(Long.valueOf(beforeStatuses[i]));
			entity.setCreatePeople(user.getUserId());
			entity.setJgId(idses[i]);
			entity.setReason(rs);
			entity.setStatusId(i+"");
			entity.setCreateTime(Calendar.getInstance().getTime());
			entities.add(entity);
		}
		materialKcjgDao.changeStatus(entities);
		materialKcjgDao.examine(ids,status);
	}


	@Override
	public List<MaterialkcjgStatus> findStatus(String jgId) {
		// TODO Auto-generated method stub
		return materialKcjgDao.findStatus(jgId);
	}


	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		materialKcjgDao.delByIds(delIds);
	}


	@Override
	public void update(MaterialKcjg materialKcjg, Long status) {
		// TODO Auto-generated method stub
		materialKcjgDao.update(materialKcjg);
		List<MaterialkcjgStatus> entities = new ArrayList<MaterialkcjgStatus>();
		MaterialkcjgStatus entity = new MaterialkcjgStatus();
		entity.setAfterStatus(Long.valueOf(0));
		entity.setBeforeStatus(status);
		entity.setCreatePeople(materialKcjg.getCreatePeople());
		entity.setJgId(materialKcjg.getJgId());
		entity.setStatusId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		entities.add(entity);
		materialKcjgDao.changeStatus(entities);
	}


	@Override
	public void save(MaterialKcjg materialKcjg) {
		// TODO Auto-generated method stub
		materialKcjgDao.save(materialKcjg);
	}


	@Override
	public MaterialKcjg findById(String jgId) {
		// TODO Auto-generated method stub
		return materialKcjgDao.findById(jgId);
	}


	@Override
	public List<MaterialKcjgVo> findByVo2(MaterialKcjgQueryVo mkqv, Units units) {
		// TODO Auto-generated method stub
		List<MaterialKcjg> list = materialKcjgDao.findByVo(mkqv,units);
		List<MaterialKcjgVo> materialKcjgVos = new ArrayList<MaterialKcjgVo>();
		for(MaterialKcjg m :list){
			MaterialKcjgVo materialKcjgVo = new MaterialKcjgVo(m);
			UserInfo userInfo  = userInfoDao.findById(m.getCreatePeople());
			materialKcjgVo.setUserName(userInfo.getUserName());
			if(userInfo.getUnitsId()!=null){
			Units unitsInfo = unitsDao.findById(userInfo.getUnitsId());
			if(unitsInfo!=null){
				materialKcjgVo.setUnitsLevel(unitsInfo.getUnitsLevel());
				materialKcjgVo.setUnitsName(unitsInfo.getUnitsName());
				materialKcjgVo.setIndustry(unitsInfo.getIndustry());
				materialKcjgVo.setUnitsId(unitsInfo.getUnitsId());
				if(unitsInfo.getFunitsId()!=null){
					Units funitsInfo = unitsDao.findById(unitsInfo.getFunitsId());
					materialKcjgVo.setFunitsName(funitsInfo.getUnitsName());
					materialKcjgVo.setFunitsId(funitsInfo.getUnitsId());
				}
			}
			}
			materialKcjgVos.add(materialKcjgVo);
		}
		return materialKcjgVos;
	}


	@Override
	public List<?> ajax(String nian, UserInfo user) {
		// TODO Auto-generated method stub
		
		return materialKcjgDao.ajax(nian,user);
	}


}
